package com.dodock.survey.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.dodock.survey.Brave;
import com.dodock.survey.R;
import com.dodock.survey.db.DatabaseManager;

public class LoginActivity extends Activity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		String filePath = getFilesDir().getAbsolutePath();
		Brave.setFilePath(filePath);
		
		Brave.setExternalSdCardPath(Environment.getExternalStorageDirectory()+"/");
		DatabaseManager db = new DatabaseManager(this);
		db.initializeDatabase();
		
		Brave.setDatabaseManager(db);
		Button loginBtn = (Button) findViewById(R.id.loginBtn);
		loginBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				EditText id = (EditText) findViewById(R.id.userId);
				EditText pass = (EditText) findViewById(R.id.userPassword);
				if(id.getText().length() <= 0)
				{
					openErrorDialog("Please enter user id.");
					id.requestFocus();
					return;
				}
				if(id.getText().length() <= 0)
				{
					openErrorDialog("Please enter password.");
					pass.requestFocus();
					return;
				}
				startActivity( new Intent( LoginActivity.this, HomeActivity.class));
				finish();
			}
		});
	}
	
	private void openErrorDialog(String err_msg){
		
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setMessage(err_msg);
		alert.setCancelable(true);

		alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
			}
		});

		alert.show();
	}

}
